package servlet.man;

import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.StuInfoMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李夏
 * @version 1.0
 */
//管理员获取某年级在校离校情况(2020-2023),2020为大四

@WebServlet("/man/onLeaveSchool")
public class OnLeaveSchoolServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer grade =Integer.valueOf(request.getParameter("grade"));
        grade=4-(grade%2020);
        List<Integer> list=new ArrayList<>();
        List<Integer>list1= null;
        try {
            list1 = StuInfoMapper.queryOnLeaveSchool(grade,list);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        Result result = new Result();
        if(list1!=null){
            result.success(list1);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }
    }
}
